﻿/**
* @description  Class to handle drawing and styling of static textfields.
*/
package com.foed.text {

	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getQualifiedClassName;

	import com.foed.Alignment;
	import com.foed.UIObject;
	import com.foed.graphic.StyleFormat;
	import com.foed.managers.LabelManager;
	
	public class Label extends UIObject {
	
		private static const CLASS:String = getQualifiedClassName(Label);

		private var _text:String;
		private var _field:TextField;
		private var _vAlign:String;
		private var _hAlign:String;
	
		/**
		* @description	Initializes component properties.
		*/	
		override protected function init():void {
			super.init();
			_classes.push(Label.CLASS);
			_vAlign = Alignment.TOP;
			_hAlign = Alignment.LEFT;
			makeLabel();
		}

		/**
		* @description	Draws component initially.
		*/	
		override public function initialize():void {
			super.initialize();
			if (_text != null) text = _text;
		}

		/**
		* @description	Embeds the font in the field, if necessary, and reapplies the TextFormat.
		*/	
		override protected function draw():void {
			_field.embedFonts = getStyleFormat().getProperty("embedFonts") as Boolean;
			setTextFormat(getStyleFormat().getProperty("textFormat") as TextFormat);
		}

		/**
		* @description	Creates a TextField and initializes its properties.
		*/	
		private function makeLabel():void {
			_field = new TextField();
			addChild(_field);
			_field.selectable = false;
			_field.autoSize = TextFieldAutoSize.LEFT;
		}

		/**
		* @description	Positions the textfield within the label based on its alignment settings.
		*/	
		private function positionLabel():void {
			switch (_vAlign) {
				case Alignment.BOTTOM:
					_field.y = (height - _field.height)|0;
					break;
				case Alignment.MIDDLE:
					_field.y = ((height - _field.height)/2)|0;
					break;
				default:
					_field.y = 0;
			}
			switch (_hAlign) {
				case Alignment.RIGHT:
					_field.x = (width - _field.width)|0;
					break;
				case Alignment.CENTER:
					_field.x = ((width - _field.width)/2)|0;
					break;
				default:
					_field.x = 0;
			}
		}

		/**
		* @description  Adds the text to the textfield and repositions the field.
		*/
		private function addText():void {
			_field.text = LabelManager.getInstance().getLabel(_text);
			positionLabel();
		}
	
		/**
		* @description  Event handler for when teh LabelManager is ready with labels.
		*
		* @param  pEvent  Event fired by LabelManager.
		*/
		private function onLabelsLoaded(pEvent:Event):void {
			LabelManager.getInstance().removeEventListener(Event.COMPLETE, onLabelsLoaded);
			addText();
		}

		/**
		* @description	Sets the width and height of component.
		*
		* @param  pWidth  Width of component.
		* @param  pHeight  Height of component.
		*/	
		override public function setSize(pWidth:Number, pHeight:Number):void {
			super.setSize(pWidth, pHeight);
			if (_initialized) positionLabel();
		}

		/**
		* @description	Sets the styleFormat instance to apply to component.
		*
		* @param  pFormat  Instance of StyleFormat to apply to component.
		*/
		override public function setStyleFormat(pStyleFormat:StyleFormat):void {
			super.setStyleFormat(pStyleFormat);
			if (_initialized) draw();
		}

		/**
		* @description  Sets current and new text format for text,
		*				corresponding to TextField.setTextFormat and TextField.defaultTextFormat.
		*
		* @param  pNewFormat  The TextFormat instance to apply.
		* @param  pBeginIndex  The starting character index for the selection to apply the format to.
		* @param  pEndIndex  The ending character index for the selection to apply the format to.
		*/
		public function setTextFormat(pNewFormat:TextFormat, pBeginIndex:Number=-1, pEndIndex:Number=-1):void {
			if (!_initialized) return;
			_field.setTextFormat(pNewFormat, pBeginIndex, pEndIndex);
			_field.defaultTextFormat = pNewFormat;
			if (_text.length > 0) positionLabel();
		}
	
		/**
		* @description	Returns the current text in the label.
		*
		* @returns  The current text in the label.
		*/	
		public function get text():String {
			return _text;
		}
	
		/**
		* @description	Sets the current text for the label.
		*
		* @param  pText  The text to apply to the label.
		*/	
		public function set text(pText:String):void {
			_text = pText;
			if (!_initialized) return;
			var pLabelManager:LabelManager = LabelManager.getInstance();
			if (pLabelManager.ready) {
				addText();
			} else {
				pLabelManager.addEventListener(Event.COMPLETE, onLabelsLoaded);
			}
		}
	
		/**
		* @description  Returns the vertical alignment for the textfield in the label.
		*
		* @returns  The vertical alignment for the textfield in the label.
		*/
		public function get vAlign():String {
			return _vAlign;
		}
	
		/**
		* @description  Sets the vertical alignment for the textfield in the label.
		*
		* @param  pAlign  The vertical alignment for the textfield in the label.
		*/
		public function set vAlign(pAlign:String):void {
			_vAlign = pAlign;
			if (_initialized) positionLabel();
		}
	
		/**
		* @description  Returns the horizontal alignment for the textfield in the label.
		*
		* @returns  The horizontal alignment for the textfield in the label.
		*/
		public function get hAlign():String {
			return _hAlign;
		}
	
		/**
		* @description  Sets the horizontal alignment for the textfield in the label.
		*
		* @param  pAlign  The horizontal alignment for the textfield in the label.
		*/
		public function set hAlign(pAlign:String):void {
			_hAlign = pAlign;
			if (_initialized) positionLabel();
		}
	
	}

}